package com.ljchen.formalProject.frame.shiro.realm;

import com.ljchen.formalProject.enums.ResultCode;
import com.ljchen.formalProject.frame.exceptions.APIException;
import com.ljchen.formalProject.frame.shiro.jwt.JwtToken;
import com.ljchen.formalProject.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

@Slf4j
public class TokenValidateRealm extends AuthorizingRealm {


    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String token = (String) authenticationToken.getCredentials();
        if (JwtUtil.verifyToken(token)) {
            return new SimpleAuthenticationInfo(token, token, this.getName());
        }
        throw new APIException(ResultCode.ERROR, "token认证失败");
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


}
